<?php

/**
 * alltosun.com 通知widget notification.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 石武浩 (shiwh@alltosun.com) $
 * $Date:  2014-6-23 下午2:21:59 $
 * $Id$
*/

class notification_widget
{
    private $per_page = 10;
    /**
     * 创建通知
     * @param array $params
     * @return int 通知id
     */
    public function create($params)
    {
        if (empty($params) || !is_array($params)) {
            return false;
        }

        $info = array(
                    'company_id'      => 0,
                    'res_name'        => '',
                    'user_id'         => 0,
                    'to_user_id'      => 0,
                    'content'         => ''
                );

        foreach ($info as $k=>$v) {
            if (!isset($params[$k]) || empty($params[$k])) {
                continue;
            }

            $info[$k] = $params[$k];
            if (!is_numeric($info[$k])) {
                $info[$k] = strip_tags($info[$k], '<a>');
            }
        }

        if (empty($info['content'])) {
            return '通知内容不能为空';
        }
        if (empty($info['to_user_id'])) {
            return '通知接收方不能为空';
        }
        if (empty($info['company_id'])) {
            $info['company_id'] = company_helper::get_company_id();
        }

        if ($params['res_name'] == 'system') {
            $field = 'system';
        } else {
            $field = 'app';
        }

        // 创建提示消息
        message_helper::update_message_unread_num($info['to_user_id'], $field, 'add', 1);
        return _model('notification')->create($info);
    }

    /**
     * 取得某人的通知
     * @param array $params
     * @return array
     */
    public function get_list($params)
    {
        $per_page = $this->per_page;
        $page = Request::getParam('page_no', 1);

        if (empty($params) || !is_array($params)) {
            return array();
        }

        $filter = array();
        if (isset($params['to_user_id']) && !empty($params['to_user_id'])) {
            $filter['to_user_id'] = $params['to_user_id'];
        }
        if (isset($params['type']) && $params['type'] == 1) {
            $filter['res_name'] = 'system';
        } else {
            $params['type'] = 2;
            $filter['res_name'] = notification_config::$notification_type;
        }
        if (isset($params['user_id']) && !empty($params['user_id'])) {
            $filter['user_id'] = $params['user_id'];
        }
        if (isset($params['is_read'])) {
            $filter['is_read'] = $params['is_read'];
        }
        if (isset($params['is_confirm'])) {
            $filter['is_confirm'] = $params['is_confirm'];
        }

        if (empty($filter)) {
            return array();
        }

        $filter['status'] = 1;

        $count = _model('notification')->getTotal($filter);
        $list = array();
        if ($count) {
            $pager = new Pager($per_page);
            $list = _model('notification')->getList($filter, 'ORDER BY `id` DESC '.$pager->getLimit($page));

            if ($pager->generate($count)) {
                Response::assign('pager', $pager);
            }
        }

        foreach ($list as $k => $v){
            $user_info = user_helper::get_user_info($v['user_id']);
            $list[$k]['user_name'] = $user_info['user_name'];
            $list[$k]['user_id'] = $user_info['id'];
        }

        Response::assign('type', $params['type']);
        Response::assign('list', $list);

        return $list;
    }
}
?>